package com.hzw.saas.common.pay.unionpay.config;

import javax.annotation.PostConstruct;

import cn.hutool.core.util.StrUtil;
import lombok.AccessLevel;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

/**
 * @author zzl
 * @since 03/18/2021
 */
// @Component()
// @Scope(ConfigurableBeanFactory.SCOPE_SINGLETON)
// @ConditionalOnProperty(name = "unionpay.frontUrl", matchIfMissing = true)
// @ConfigurationProperties(prefix = "unionpay", ignoreInvalidFields = true, ignoreUnknownFields = true)
@Data
@Slf4j
public class UnionPayConfig {

    @Getter(AccessLevel.NONE)
    @Setter(AccessLevel.NONE)
    private static UnionPayConfig instance;

    private String dataFormat = "yyyyMMddHHmmss";
    private String encoding = "UTF-8";

    /*************交易参数**************/
    /**
     * 商户号码，申请的商户号或者open上注册得来的777商户号
     */
    private String merId;

    /*************通知地址**************/
    /**
     * frontUrl
     */
    private String frontUrl;
    /**
     * backUrl
     */
    private String backUrl;
    /**
     * frontFailUrl
     */
    private String frontFailUrl;

    /*************交易地址**************/
    /**
     * 前台请求URL.
     */
    private String frontTransUrl;
    /**
     * 后台请求URL.
     */
    private String backTransUrl;
    /**
     * 二维码统一下单请求URL.
     */
    private String orderTransUrl;
    /**
     * 单笔查询
     */
    private String singleQueryUrl;
    /**
     * 批量查询
     */
    private String batchQueryUrl;
    /**
     * 批量交易
     */
    private String batchTransUrl;
    /**
     * 文件传输
     */
    private String fileTransUrl;
    /**
     * 有卡交易.
     */
    private String cardTransUrl;
    /**
     * app交易
     */
    private String appTransUrl;

    /*************证书配置**************/
    /**
     * 签名证书路径.
     */
    private String signCertPath;
    /**
     * 签名证书密码.
     */
    private String signCertPwd;
    /**
     * 签名证书类型.
     */
    private String signCertType;
    /**
     * 加密公钥证书路径.
     */
    private String encryptCertPath;
    /**
     * 磁道加密证书路径.
     */
    private String encryptTrackCertPath;
    /**
     * 磁道加密公钥模数.
     */
    private String encryptTrackKeyModulus;
    /**
     * 磁道加密公钥指数.
     */
    private String encryptTrackKeyExponent;
    /**
     * 验证签名公钥证书目录.
     */
    private String validateCertDir;
    /**
     * 证书使用模式(单证书/多证书)
     */
    private String singleMode;
    /**
     * 安全密钥(SHA256和SM3计算时使用)
     */
    private String secureKey;
    /**
     * 根证书路径
     */
    private String rootCertPath;
    /**
     * 中级证书路径
     */
    private String middleCertPath;
    /**
     * 是否验证验签证书CN，除了false都验
     */
    private boolean ifValidateCnName = false;
    /**
     * 是否验证https证书，默认都不验
     */
    private boolean ifValidateRemoteCert = false;
    /**
     * signMethod，没配按01吧
     */
    private String signMethod = "01";
    /**
     * version，没配按5.0.0
     */
    private String version = "5.0.0";
    /**
     * 按照商户代码读取指定签名证书目录.
     */
    private String signCertDir;


    /*缴费相关地址*/
    private String jfFrontTransUrl;
    private String jfBackTransUrl;
    private String jfSingleQueryUrl;
    private String jfCardTransUrl;
    private String jfAppTransUrl;

    private String qrcBackTransUrl;
    private String qrcB2cIssBackTransUrl;
    private String qrcB2cMerBackTransUrl;

    //综合认证
    private String zhrzFrontTransUrl;
    private String zhrzBackTransUrl;
    private String zhrzSingleQueryUrl;
    private String zhrzCardTransUrl;
    private String zhrzAppTransUrl;
    private String zhrzFaceTransUrl;

    @PostConstruct
    private void init() {
        this.verifyParams();
        UnionPayConfig.instance = this;
    }

    private void verifyParams() {
        String message = "获取银联配置参数<{}>: {}";
        String errorMsg = "获取银联配置参数<{}>异常";
        log.debug("加载银联支付参数...");
        // frontUrl
        if (StrUtil.isBlank(this.frontUrl)) {
            log.error(errorMsg, "frontUrl");
        } else {
            log.info(message, "frontUrl", this.frontUrl);
        }
        // backUrl
        if (StrUtil.isBlank(this.backUrl)) {
            log.error(errorMsg, "backUrl");
        } else {
            log.info(message, "backUrl", this.backUrl);
        }
        // merId
        if (StrUtil.isBlank(this.merId)) {
            log.error(errorMsg, "merId");
        } else {
            log.info(message, "merId", this.merId);
        }
        // version
        if (StrUtil.isBlank(this.version)) {
            log.error(errorMsg, "version");
        } else {
            log.info(message, "version", this.version);
        }
        // signMethod
        if (StrUtil.isBlank(this.signMethod)) {
            log.error(errorMsg, "signMethod");
        } else {
            log.info(message, "signMethod", this.signMethod);
        }
        // ifValidateCnName
        log.info(message, "ifValidateCnName", this.ifValidateCnName);
        // ifValidateRemoteCert
        log.info(message, "ifValidateRemoteCert", this.ifValidateRemoteCert);
        // signCertPath
        if (StrUtil.isBlank(this.signCertPath)) {
            log.error(errorMsg, "signCertPath");
        } else {
            log.info(message, "signCertPath", this.signCertPath);
        }
        // signCertPwd
        if (StrUtil.isBlank(this.signCertPwd)) {
            log.error(errorMsg, "signCertPwd");
        } else {
            log.info(message, "signCertPwd", this.signCertPwd);
        }
        // signCertType
        if (StrUtil.isBlank(this.signCertType)) {
            log.error(errorMsg, "signCertType");
        } else {
            log.info(message, "signCertType", this.signCertType);
        }
        // encryptCertPath
        if (StrUtil.isBlank(this.encryptCertPath)) {
            log.error(errorMsg, "encryptCertPath");
        } else {
            log.info(message, "encryptCertPath", this.encryptCertPath);
        }
        // rootCertPath
        if (StrUtil.isBlank(this.rootCertPath)) {
            log.error(errorMsg, "rootCertPath");
        } else {
            log.info(message, "rootCertPath", this.rootCertPath);
        }
        // middleCertPath
        if (StrUtil.isBlank(this.middleCertPath)) {
            log.error(errorMsg, "middleCertPath");
        } else {
            log.info(message, "middleCertPath", this.middleCertPath);
        }

        log.debug("加载银联支付参数完成.");
    }

    public static UnionPayConfig config() {
        return instance;
    }
}
